/**
 * 
 */
package server.alarmThreads;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Scanner;

import org.apache.log4j.Logger;

import server.config.ServerConfig;
import server.container.EinsatzAlarm;

/**
 * @author Christian
 * 
 */
public class AlarmappAlarmierer implements Runnable {

	private EinsatzAlarm alarm = null;

	private static Logger logger = Logger.getLogger(AlarmappAlarmierer.class);

	public AlarmappAlarmierer(EinsatzAlarm alarm) {
		this.alarm = alarm;
	}

	/**
	 * @param alarm
	 */
	public void alarmiere() {

		Thread t = new Thread(this);
		t.start();

	}

	/**
	 * 
	 * @author Christian
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {

		StringBuilder exceBuilder = new StringBuilder();
		exceBuilder.append(ServerConfig.getInstance().getAlarmAppToolPath());
		exceBuilder.append("AlarmPushTool.exe");
		if(alarm.isProbeAlarm()) {
			exceBuilder.append(" -c probe");
		}
		else {
			exceBuilder.append(" -c vollalarmleu");
		}
		exceBuilder.append(" -t ");
		exceBuilder.append(alarm.getEinsatzArt());
		exceBuilder.append(" -m ");
		exceBuilder.append(alarm.toString());
		exceBuilder.append(" -p ");
		exceBuilder.append(ServerConfig.getInstance().getAlarmAppToolPath());
		Runtime runtime = Runtime.getRuntime();
		try {

			if (!ServerConfig.getInstance().isDebug()) {

				Process run = runtime.exec(exceBuilder.toString());

				BufferedInputStream rtm = new BufferedInputStream(run.getInputStream());

				Scanner sc = new Scanner(rtm);

				String output = "";

				while (sc.hasNext()) {

					output = output + sc.next() + " ";

				}

				logger.info(output);

				sc.close();
			}
			else {
				logger.info(exceBuilder);
			}
			logger.info("Alarmapp alarmiert!");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
